﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Purchase;
using EB.BPARequestManagement;

public partial class Modules_Purchase_SerchPyrchaseRequest : System.Web.UI.UserControl
{
    RequestCreate requestLink = new RequestCreate();
    Purchase.Purchase purchase = new Purchase.Purchase();
    DataSet ds = new DataSet();
    SearchCriteria advanceSearch = new SearchCriteria();
    int requestTypeId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        string requestType = Request.QueryString["reqType"].ToString();
        if (requestType != "")
            requestTypeId = Convert.ToInt16(requestType);
        else
            requestTypeId = 1;
        ImageButton2.Enabled = false;
        txtDate.Enabled = false;
        ImageButton1.Enabled = false;
        ddlSelect.Enabled = false;
        ddlStatus.Enabled = false;
        txtDateOfCreation.Enabled = false;
        txtProductName.Enabled = false;
        txtrequestor.Enabled = false;

        if (!IsPostBack)
        {
            ddlSelect.Items.Add("Between");
            ddlSelect.Items.Add("<=");
            ddlSelect.Items.Add(">=");
            ddlSelect.Items.Add("=");
            ddlSelect.Items.Insert(0, "-Select-");

            if (requestTypeId == 1)
            {
                
        //DataSet res = purchase.GetProductionWiseSearch();
        //grvPurchasedDetails.DataSource = res;
        //grvPurchasedDetails.DataBind();

                bool res = purchase.SearchPurchaseRequest(advanceSearch, out ds);
                grvPurchasedDetails.DataSource = ds;
                grvPurchasedDetails.DataBind();
            }
            else
            {
                bool res = purchase.SearchGeneralPurchaseRequest(advanceSearch, out ds);
                grvGeneralSearch.DataSource = ds;
                grvGeneralSearch.DataBind();

            }

            GetStatus();
            //GetPurchasedRequestDetails();
        }
    }
    protected void GetStatus()
    {
        DataSet statusDS = requestLink.GetRequestListStatus();
        ddlStatus.DataSource = statusDS;
        ddlStatus.DataValueField = "Status";
        ddlStatus.DataTextField = "StatusNote";
        ddlStatus.DataBind();
        ddlStatus.Items.Insert(0, "-Select-");
    }
    protected void grvGeneralSearch_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
         int reqid = 0;
        int statusid = 0;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView gv = (GridView)e.Row.FindControl("GridView22");
            string gg = ((Label)e.Row.FindControl("lbluId1")).Text;
            string status = ((Label)e.Row.FindControl("lblStatusId1")).Text;
            if (status != "")
                statusid = Convert.ToInt16(status);
            if (gg != "")
                reqid = Convert.ToInt16(gg);
            DataSet test = purchase.GetMaterialDetailsByRequestId(reqid);

            if (statusid != 1)
            {
                ((LinkButton)e.Row.FindControl("lbStatusGeneral")).Enabled = false;
                ((LinkButton)e.Row.FindControl("lbStatusGeneral")).OnClientClick = "";
            }
            gv.DataSource = test;
            gv.DataBind();
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");

            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

        }
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";

        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";
    }
    
    protected void gv_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        
        int reqid = 0;
        int statusid = 0;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView gv = (GridView)e.Row.FindControl("GridView2");
            string gg = ((Label)e.Row.FindControl("lbluId")).Text;
            string status = ((Label)e.Row.FindControl("lblStatusId")).Text;
            if (status != "")
                statusid = Convert.ToInt16(status);
            //e.Row.FindControl("lbluId").ToString();
            // GridView gv = e.Row.FindControl("GridView2");
            //
            if (gg != "")
                reqid = Convert.ToInt16(gg);
            DataSet test = purchase.GetTest(reqid);

            if (statusid != 1)
            {
                ((LinkButton)e.Row.FindControl("lbStatus")).Enabled = false;
                ((LinkButton)e.Row.FindControl("lbStatus")).OnClientClick = "";
            }
            gv.DataSource = test;
            gv.DataBind();
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");

            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

        }
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";

        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";
    }
    protected void GetPurchasedRequestDetails()
    {
        //DataSet purchaseDS = purchase.GetPurchasedRequestDetails();
        //grvPurchasedDetails.DataSource = purchaseDS;
        grvPurchasedDetails.DataBind();
    }

    protected void AdvancedSerch()
    {
    //    DataSet ds = new DataSet();
    //    SearchCriteria advanceSearch = new SearchCriteria();

    //    advanceSearch.FirstName = txtrequestor.Text;
    //    advanceSearch.ProductName = txtProductName.Text;
    //    if (txtAmount.Text.Trim() != "")
    //        advanceSearch.DateStatus = Convert.ToInt16(txtAmount.Text);
    //    advanceSearch.DateofCreation = txtDateOfCreation.Text;
    //    bool res = purchase.SearchPurchaseRequest(advanceSearch, out ds);
    //    grvPurchasedDetails.DataSource = ds;
    //    grvPurchasedDetails.DataBind();

        if (rblCriteria.SelectedItem.ToString() == "ProductName")
        {
            txtDateOfCreation.Text = string.Empty;
            ddlSelect.SelectedIndex = 0;
            txtDate.Text = string.Empty;
            txtrequestor.Text = string.Empty;
            ddlStatus.SelectedIndex = 0;
            ImageButton2.Enabled = false;
            txtDate.Enabled = false;
            ImageButton1.Enabled = false;
            ddlSelect.Enabled = false;
            ddlStatus.Enabled = false;
            txtDateOfCreation.Enabled = false;
            txtProductName.Enabled = true;
            txtrequestor.Enabled = false;
            advanceSearch.ProductName = txtProductName.Text.Trim();
            advanceSearch.DateofCreation = string.Empty;
            advanceSearch.FirstName = string.Empty;
            advanceSearch.Status = 0;
            advanceSearch.DateStatus = "0";
        }
        else if (rblCriteria.SelectedItem.ToString() == "Requester")
        {
            txtDateOfCreation.Text = string.Empty;
            ddlSelect.SelectedIndex = 0;
            txtDate.Text = string.Empty;
            txtProductName.Text = string.Empty;
            ddlStatus.SelectedIndex = 0;
            ImageButton2.Enabled = false;
            txtDate.Enabled = false;
            ImageButton1.Enabled = false;
            ddlSelect.Enabled = false;
            ddlStatus.Enabled = false;
            txtDateOfCreation.Enabled = false;
            txtProductName.Enabled = false;
            txtrequestor.Enabled = true;
            advanceSearch.ProductName = string.Empty;
            advanceSearch.DateofCreation = string.Empty;
            advanceSearch.FirstName = txtrequestor.Text.Trim();
            advanceSearch.Status = 0;
            advanceSearch.DateStatus = "0";
        }
        else if (rblCriteria.SelectedItem.ToString() == "Status")
        {
            txtDateOfCreation.Text = string.Empty;
            ddlSelect.SelectedIndex = 0;
            txtDate.Text = string.Empty;
            txtrequestor.Text = string.Empty;
            txtProductName.Text = string.Empty;
            ImageButton2.Enabled = false;
            txtDate.Enabled = false;
            ImageButton1.Enabled = false;
            ddlSelect.Enabled = false;
            ddlStatus.Enabled = true;
            txtDateOfCreation.Enabled = false;
            txtProductName.Enabled = false;
            txtrequestor.Enabled = false;
            advanceSearch.ProductName = string.Empty;
            advanceSearch.DateofCreation = string.Empty;
            advanceSearch.FirstName = string.Empty;
            advanceSearch.DateStatus = "0";
            if (ddlStatus.SelectedIndex != 0)
                advanceSearch.Status = Convert.ToInt16(ddlStatus.SelectedValue);
            else
                advanceSearch.Status = 0;
        }
        else if (rblCriteria.SelectedItem.ToString() == "Date of Creation")
        {
            txtProductName.Text = string.Empty;
            txtrequestor.Text = string.Empty;
            ddlStatus.SelectedIndex = 0;
            
            ImageButton1.Enabled = true; ;
            ddlSelect.Enabled = true;
            ddlStatus.Enabled = false;
            txtDateOfCreation.Enabled = true;
            txtProductName.Enabled = false;
            txtrequestor.Enabled = false;
            if (ddlSelect.SelectedItem.ToString() == "Between")
            {
                ImageButton2.Enabled = true; ;
                txtDate.Enabled = true;
                advanceSearch.DateofCreation = txtDateOfCreation.Text.Trim();
                advanceSearch.DateofCreation1 = txtDate.Text.Trim();
                advanceSearch.FirstName = string.Empty;
                advanceSearch.ProductName = string.Empty;

                advanceSearch.Status = 0;
                if (ddlSelect.SelectedIndex != 0)
                    advanceSearch.DateStatus = ddlSelect.SelectedItem.ToString();
                else
                    advanceSearch.DateStatus = "0";
            }
            else
            {

                advanceSearch.DateofCreation = txtDateOfCreation.Text.Trim();
                advanceSearch.FirstName = string.Empty;
                advanceSearch.ProductName = string.Empty;

                advanceSearch.Status = 0;
                if (ddlSelect.SelectedIndex != 0)
                    advanceSearch.DateStatus = ddlSelect.SelectedItem.ToString();
                else
                    advanceSearch.DateStatus = "0";
            }
        }
        else
        {
            txtDate.Enabled = false;
            txtDateOfCreation.Text = string.Empty;
            txtProductName.Text = string.Empty;
            txtrequestor.Text = string.Empty;
            ddlStatus.SelectedIndex = 0;
            ddlStatus.Enabled = false;
            txtDateOfCreation.Enabled = false;
            txtProductName.Enabled = false;
            txtrequestor.Enabled = false;
            advanceSearch.ProductName = string.Empty;
            advanceSearch.DateofCreation = string.Empty;
            advanceSearch.FirstName = string.Empty;
            advanceSearch.Status = 0;
        }
       
   }
    protected void lbStatusGeneral_Click(object sender, EventArgs e)
    {
        LinkButton ProductIdButton = sender as LinkButton;
        string[] CommandArgument = ProductIdButton.CommandArgument.Split(',');
        int uniqId = Convert.ToInt32(CommandArgument[0].ToString());
        int requestListUID = Convert.ToInt32(CommandArgument[1].ToString());
        if (uniqId > 0)
        {

            Session["UserManager_Control"] = "Purchase/ucPurchaseOrderRequest.ascx";
            Response.Redirect("BusinessProcessAutomation.aspx?linkKey=" + uniqId + "&ReqUID=" + requestListUID + "&RequestType=" + 2);

        }
    }
    
    protected void lbStatus_Click(object sender, EventArgs e)
    {
        //LinkButton btn = (LinkButton)sender;
        //string[] CommandArgument = btn.CommandArgument.Split(',');
        //string requestNo = CommandArgument[0];
        //string requestNo1 = CommandArgument[1];

        LinkButton ProductIdButton = sender as LinkButton;
        string[] CommandArgument = ProductIdButton.CommandArgument.Split(',');
        int uniqId = Convert.ToInt32(CommandArgument[0].ToString());
        int requestListUID = Convert.ToInt32(CommandArgument[1].ToString());
        if (uniqId > 0)
        {

            Session["UserManager_Control"] = "Purchase/ucPurchaseOrderRequest.ascx";
            Response.Redirect("BusinessProcessAutomation.aspx?linkKey=" + uniqId + "&ReqUID="+ requestListUID +"&RequestType=" + 1);

        }
    }

    protected void rblCriteria_SelectedIndexChanged(object sender, EventArgs e)
    {
        AdvancedSerch();
        //if (rblCriteria.SelectedItem.ToString() == "ProductName")
        //{
        //    txtDate.Text = string.Empty;
        //    txtrequestor.Text = string.Empty;
        //    ddlSelect.SelectedIndex = 0;
        //    ddlStatus.SelectedIndex = 0;
        //    txtDateOfCreation.Text = string.Empty;

        //    ImageButton2.Enabled = false;
        //    txtDate.Enabled = false;
        //    ImageButton1.Enabled = false;
        //    ddlSelect.Enabled = false;
        //    ddlStatus.Enabled = false;
        //    txtDateOfCreation.Enabled = false;
        //    txtProductName.Enabled = true;
        //    txtrequestor.Enabled = false;
        //    advanceSearch.ProductName = txtProductName.Text.Trim();
        //    advanceSearch.DateofCreation = string.Empty;
        //    advanceSearch.FirstName = string.Empty;
        //    advanceSearch.Status = 0;
        //    advanceSearch.DateStatus = "0";
        //}
        //else if(rblCriteria.SelectedItem.ToString() == "Requester")
        //{
        //    txtDate.Text = string.Empty;
        //    txtProductName.Text = string.Empty;
        //    ddlSelect.SelectedIndex = 0;
        //    ddlStatus.SelectedIndex = 0;
        //    txtDateOfCreation.Text = string.Empty;

        //    ImageButton2.Enabled = false;
        //    txtDate.Enabled = false;
        //    ImageButton1.Enabled = false;
        //    ddlSelect.Enabled = false;
        //    ddlStatus.Enabled = false;
        //    txtDateOfCreation.Enabled = false;
        //    txtProductName.Enabled = false;
        //    txtrequestor.Enabled = true;
        //    advanceSearch.ProductName = string.Empty;
        //    advanceSearch.DateofCreation = string.Empty;
        //    advanceSearch.FirstName = txtrequestor.Text.Trim();
        //    advanceSearch.Status = 0;
        //    advanceSearch.DateStatus = "0";
        //}
        //else if (rblCriteria.SelectedItem.ToString() == "Status")
        //{
        //    txtDate.Text = string.Empty;
        //    txtProductName.Text = string.Empty;
        //    ddlSelect.SelectedIndex = 0;
        //    txtrequestor.Text = string.Empty;
        //    txtDateOfCreation.Text = string.Empty;

        //    ImageButton2.Enabled = false;
        //    txtDate.Enabled = false;
        //    ImageButton1.Enabled = false;
        //    ddlSelect.Enabled = false;
        //    ddlStatus.Enabled = true;
        //    txtDateOfCreation.Enabled = false;
        //    txtProductName.Enabled = false;
        //    txtrequestor.Enabled = false;
        //    advanceSearch.ProductName = string.Empty;
        //    advanceSearch.DateofCreation = string.Empty;
        //    advanceSearch.FirstName = string.Empty;
        //    advanceSearch.DateStatus = "0";
        //    if (ddlStatus.SelectedIndex != 0)
        //        advanceSearch.Status = Convert.ToInt16(ddlStatus.SelectedValue);
        //    else
        //        advanceSearch.Status = 0;
        //}
        //else if (rblCriteria.SelectedItem.ToString() == "Date of Creation")
        //{
        //    txtProductName.Text = string.Empty;
        //    ddlSelect.SelectedIndex = 0;
        //    txtrequestor.Text = string.Empty;
        //    ddlStatus.SelectedIndex = 0;

        //    ImageButton1.Enabled = true; ;
        //    ddlSelect.Enabled = true;
        //    ddlStatus.Enabled = false;
        //    txtDateOfCreation.Enabled = true;
        //    txtProductName.Enabled = false;
        //    txtrequestor.Enabled = false;
        //    advanceSearch.ProductName = string.Empty;
        //    if (ddlSelect.SelectedItem.ToString() == "Between")
        //    {
        //        ImageButton2.Enabled = true; ;
        //        txtDate.Enabled = true;
        //        advanceSearch.DateofCreation1 = txtDate.Text.Trim();
        //        advanceSearch.DateofCreation = txtDateOfCreation.Text.Trim();
        //        advanceSearch.FirstName = string.Empty;
        //        advanceSearch.ProductName = string.Empty;

        //        advanceSearch.Status = 0;
        //        if (ddlSelect.SelectedIndex != 0)
        //            advanceSearch.DateStatus = ddlSelect.SelectedItem.ToString();
        //        else
        //            advanceSearch.DateStatus = "0";
        //    }
        //    else
        //    {
        //        txtDate.Text = string.Empty;
        //        advanceSearch.DateofCreation = txtDateOfCreation.Text.Trim();
        //        advanceSearch.FirstName = string.Empty;
        //        advanceSearch.Status = 0;
        //        if (ddlSelect.SelectedIndex != 0)
        //            advanceSearch.DateStatus = ddlSelect.SelectedItem.ToString();
        //        else
        //            advanceSearch.DateStatus = "0";
        //    }
        //}
        //else
        //{
        //    txtDate.Text = string.Empty;
        //    txtDate.Enabled = false;
        //    txtDateOfCreation.Text = string.Empty;
        //    txtProductName.Text = string.Empty;
        //    txtrequestor.Text = string.Empty;
        //    ddlStatus.SelectedIndex = 0;
        //    ddlStatus.Enabled = false;
        //    txtDateOfCreation.Enabled = false;
        //    txtProductName.Enabled = false;
        //    txtrequestor.Enabled = false;
        //    advanceSearch.ProductName = string.Empty;
        //    advanceSearch.DateofCreation = string.Empty;
        //    advanceSearch.FirstName = string.Empty;
        //    advanceSearch.Status = 0;
        //}
       

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (rblCriteria.SelectedItem!=null)
        {
            AdvancedSerch();
        }
        if (requestTypeId == 1)
        {
            bool res = purchase.SearchPurchaseRequest(advanceSearch, out ds);
            grvPurchasedDetails.DataSource = ds;
            grvPurchasedDetails.DataBind();
        }
        else
        {
            bool res = purchase.SearchGeneralPurchaseRequest(advanceSearch, out ds);
            grvGeneralSearch.DataSource = ds;
            grvGeneralSearch.DataBind();
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "Purchase/ucPurchaseOrderRequest.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    protected void ddlSelect_SelectedIndexChanged(object sender, EventArgs e)
    {
   
       ImageButton1.Enabled = true; ;
       ddlSelect.Enabled = true;
       ddlStatus.Enabled = false;
       txtDateOfCreation.Enabled = true;
       txtProductName.Enabled = false;
       txtrequestor.Enabled = false;
       advanceSearch.ProductName = string.Empty;
       txtDate.Text = string.Empty;
       if (ddlSelect.SelectedItem.ToString() == "Between")
       {
           ImageButton2.Enabled = true; ;
           txtDate.Enabled = true;
           advanceSearch.DateofCreation1 = txtDate.Text.Trim();
       }
       else
       {

           advanceSearch.DateofCreation = txtDateOfCreation.Text.Trim();
           advanceSearch.FirstName = string.Empty;
           advanceSearch.Status = 0;
           if (ddlSelect.SelectedIndex != 0)
               advanceSearch.DateStatus = ddlSelect.SelectedItem.ToString();
           else
               advanceSearch.DateStatus = "0";
       }
    }

    protected void grvPurchasedDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //grvPurchasedDetails.PageIndex = e.NewPageIndex;
        //AdvancedSerch();
        //bool res = purchase.SearchPurchaseRequest(advanceSearch, out ds);
        //grvPurchasedDetails.DataSource = ds;
        //grvPurchasedDetails.DataBind();
    }
    protected void grvGeneralSearch_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       // grvPurchasedDetails.PageIndex = e.NewPageIndex;
        AdvancedSerch();
        bool res = purchase.SearchGeneralPurchaseRequest(advanceSearch, out ds);
        grvGeneralSearch.DataSource = ds;
        grvGeneralSearch.DataBind();

    }
    protected void grvGeneralSearch_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {
        grvGeneralSearch.PageIndex = e.NewPageIndex;
        bool res = purchase.SearchGeneralPurchaseRequest(advanceSearch, out ds);
        grvGeneralSearch.DataSource = ds;
        grvGeneralSearch.DataBind();
    }
}
